package com.yunwaikeji.gy_tool_library.utils.http;

import com.yunwaikeji.gy_tool_library.utils.GYLogUtils;
import com.yunwaikeji.gy_tool_library.utils.GYUiUtils;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;

public class GYHttpTask extends GYHttpAsyncTask<GYHttpTask, GYHttpModel<?>> {
    private GYHttpListener<String> httpListener;

    public GYHttpTask setHttpResultListener(GYHttpListener<String> httpListener) {
        this.httpListener = httpListener;
        return this;
    }

    public GYHttpTask() {
        setHttpListener(new GYHttpListener<HttpURLConnection>() {
            @Override
            public void start() {
                if (httpListener != null) {
                    httpListener.start();
                }
            }

            @Override
            public void stop() {
                if (httpListener != null) {
                    httpListener.stop();
                }
            }

            @Override
            public void success(HttpURLConnection httpURLConnection) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8);
                            int ch;
                            StringBuilder stringBuilder = new StringBuilder();
                            while ((ch = inputStreamReader.read()) != -1) {
                                stringBuilder.append((char) ch);
                            }
                            GYLogUtils.printJson("gylibrary网络框架", "响应\n" + httpURLConnection.getURL().toString(), stringBuilder.toString());
                            onSuccess(stringBuilder.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                            GYLogUtils.printJson("gylibrary网络框架", "响应\n" + httpURLConnection.getURL().toString(), e.toString());
                            onError(e.toString());
                        } finally {
                            if (httpURLConnection != null) {
                                httpURLConnection.disconnect();
                            }
                        }
                    }
                }).start();
            }

            @Override
            public void error(String error) {
                if (httpListener != null) {
                    httpListener.error(error);
                }
            }

            @Override
            public void finish() {
                if (httpListener != null) {
                    httpListener.finish();
                }
            }
        });
    }

    public void onSuccess(String response) {
        if (httpListener != null) {
            if (isResponseMainThread()) {
                GYUiUtils.runOnUiThread(() -> httpListener.success(response));
            } else {
                httpListener.success(response);
            }
        }
    }
}
